﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using Model;
using System.Data;
using System.Data.SqlClient;

namespace BLL
{
    public class ModelConfigBLL : BaseBLL
    {
        public ModelConfigBLL(LoginInfo info)
        {
            loginInfo = info;
        }

        public ModelConfig GetItem(string id)
        {
            return new ModelConfigDAL(loginInfo).GetItem(id);
        }
        public DataTable GetModelTbItem(string id, string modelID)
        {
            return new ModelConfigDAL(loginInfo).GetModelTbItem(id, modelID);
        }
        public DictionaryInfo GetModelType(string guid)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.GetModelType(guid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<ModelConfig> GetRefModelList(IDataParameter[] queryParas, string sort, string order)
        {
            return new ModelConfigDAL(loginInfo).GetRefModelList(queryParas, sort, order);
        }
        public IList<ModelConfig> GetList(IDataParameter[] queryParas)
        {
            return new ModelConfigDAL(loginInfo).GetList(queryParas);
        }
        public IList<ModelConfig> GetVersionList(IDataParameter[] queryParas)
        {
            return new ModelConfigDAL(loginInfo).GetVersionList(queryParas);
        }
        public IList<ModelViewConfig> GetTableInfo(string schemaName, string tableName)
        {
            return new ModelConfigDAL(loginInfo).GetTableInfo(schemaName, tableName);
        }
        public IList<ModelViewConfig> GetModelColumn(string id, string version)
        {
            return new ModelConfigDAL(loginInfo).GetModelColumn(id, version);
        }
        public IList<ModelColumnExpression> GetColumnExpressionByModelID(string id, string version)
        {
            return new ModelConfigDAL(loginInfo).GetColumnExpressionByModelID(id, version);
        }
        public int RunScript(string modelID)
        {
            return new ModelConfigDAL(loginInfo).RunScript(modelID);
        }
        /// <summary>
        /// 更新ModelType表记录
        /// </summary>
        /// <param name="dictionaryInfo"> </param>
        public int UpdateModelType(Dictionary<string, object> dict_para)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.UpdateModelType(dict_para);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int CheckModelName(string modelName,string id)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.CheckModelName(modelName,id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataTable GetModelDate(string modelID, string sort, string order, string[] sql, IDataParameter[] para)
        {
            ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
            return dal.GetModelDate(modelID, sort, order, sql.ToArray(), para);
        }
        /// <summary>
        /// 更新Model表记录
        /// </summary>
        /// <param name="dict_para"> </param>
        public int UpdateModel(Dictionary<string, object> dict_para)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.UpdateModel(dict_para);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 更新Model表记录
        /// </summary>
        /// <param name="dict_para"> </param>
        public int SendModelConfig(SqlParameter[] para)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.SendModelConfig(para);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int AddModelToType(SqlParameter[] para)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.AddModelToType(para);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int DeleteModelType(string id)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.DeleteModelType(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int RemoveModelFromType(string id)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.RemoveModelFromType(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int DeleteModel(string id)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.DeleteModel(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public int DeleteRecord(string id, string v)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.DeleteRecord(id, v);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<ModelColumnExpression> GetColumnExpression(string columnid)
        {
            return new ModelConfigDAL(loginInfo).GetColumnExpression(columnid);
        }
        public int UpdateColumn(string modelID, DataTable dt, DataTable dts)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.UpdateColumn(modelID, dt, dts);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public string CheckModelColumn(string modelID, DataTable dts)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.CheckModelColumn(modelID, dts);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataTable CheckModelScript(string text)
        {
            try
            {
                ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
                return dal.CheckModelScript(text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //========================
        public DataTable CheckDelete(string guid)
        {
            return new ModelConfigDAL(loginInfo).CheckDelete(guid);
        }

        public IList<ModelConfig> GetList(string modelTypeId, string tableName, int pageSize, int pageIndex)
        {
            return new ModelConfigDAL(loginInfo).GetList(modelTypeId, tableName, pageSize, pageIndex);
        }

        public int GetListCount(string modelTypeId, string tableName)
        {
            return new ModelConfigDAL(loginInfo).GetListCount(modelTypeId, tableName);
        }

        public bool Update(ModelConfigTransData data, int isDelete, string oldSchema, string oldTableName)
        {
            return new ModelConfigDAL(loginInfo).Update(data, isDelete, oldSchema, oldTableName);
        }

        public bool RepeatColumnCheck(string name)
        {
            return new ModelConfigDAL(loginInfo).RepeatColumnCheck(name);
        }
        public IList<ModelColumnConfigTransData> GetTransDataList(string modelTableID)
        {
            return new ModelConfigDAL(loginInfo).GetTransDataList(modelTableID);
        }

        public bool CreateView(string id, string viewName)
        {
            return new ModelConfigDAL(loginInfo).CreateView(id, viewName);
        }

        public ModelViewConfig GetViewInfo(string id)
        {
            return new ModelConfigDAL(loginInfo).GetViewInfo(id);
        }

        public bool ViewNameIsExist(string viewName, string id)
        {
            return new ModelConfigDAL(loginInfo).ViewNameIsExist(viewName, id);
        }

        public int TableNameIsExist(string tableName, string modelTableCode, string id)
        {
            return new ModelConfigDAL(loginInfo).TableNameIsExist(tableName, modelTableCode, id);
        }


        public IList<ModelColumnConfig> GetModelColumns(string modelTableID)
        {
            return new ModelConfigDAL(loginInfo).GetModelColumns(modelTableID);
        }

        public string GetFactTableName(string modelTableID)
        {
            return new ModelConfigDAL(loginInfo).GetFactTableName(modelTableID);
        }

        public IList<string> GetModelTableNameList()
        {
            return new ModelConfigDAL(loginInfo).GetModelTableNameList();
        }
        #region 模型分类关联表 Model_ConfigDictType

        public List<DictionaryInfo> GetModelTypesByModelID(string modelID)
        {
            ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
            return dal.GetModelTypesByModelID(modelID);
        }

        public List<string> GetTypeIDsByModelID(string modelID)
        {
            ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
            return dal.GetTypeIDsByModelID(modelID);
        }

        public int AddModelTypeID(string modelID, string typeID)
        {
            ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
            return dal.AddModelTypeID(modelID, typeID);
        }

        public int UpdateModelTypeID(string modelID, string typeID, string oldtypeID)
        {
            ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
            return dal.UpdateModelTypeID(modelID, typeID, oldtypeID);
        }

        public int DeleteModelTypeID(string modelID, string typeID)
        {
            ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
            return dal.DeleteModelTypeID(modelID, typeID);
        }

        public int DeleteModelTypeByModelID(string modelID)
        {
            ModelConfigDAL dal = new ModelConfigDAL(loginInfo);
            return dal.DeleteModelTypeByModelID(modelID);
        }
        #endregion
    }
}
